package zhouzhouyongche.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import zhouzhouyongche.client_util.ClientCity;
import zhouzhouyongche.client_util.Json;
import zhouzhouyongche.service.CityService;

@Controller
@Action(value = "city")
public class CityAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private CityService cityService;
	
	private int cityId;
	private String cityName;
	
	public void getCities(){
		List<ClientCity> cities = cityService.getCities();
		writeJson(cities.size(), cities);
	}
	
	public void addCity() {
		Json json = new Json();
		try {
			cityService.addCity(cityName);
			json.setSuccess(true);
			json.setMsg("添加城市成功");
		} catch (Exception e) {
			json.setSuccess(false);
			json.setMsg("添加城市失败");
		}
		writeJson(json);
	}
	
	public void editCity() {
		Json json = new Json();
		try {
			cityService.editCity(cityId, cityName);
			json.setSuccess(true);
			json.setMsg("修改城市名称成功");
		} catch (Exception e) {
			json.setSuccess(false);
			json.setMsg("修改城市名称失败");
		}
		writeJson(json);
	}
	
	public void deleteCity() {
		Json json = new Json();
		try {
			cityService.deleteCity(cityId);
			json.setSuccess(true);
			json.setMsg("删除城市名称成功");
		} catch (Exception e) {
			json.setSuccess(false);
			json.setMsg("删除城市名称失败");
		}
		writeJson(json);
	}
	
	//用于ticketAdd.jsp
	public void getCitiesWeb() {	
		List<ClientCity> cities = cityService.getCities();
		writeJson(cities);
	}

	public String getCityName() {
		return cityName;
	}

	public void setCityName(String cityName) {
		this.cityName = cityName;
	}

	public int getCityId() {
		return cityId;
	}

	public void setCityId(int cityId) {
		this.cityId = cityId;
	}

	
}
